package aop.spring.introduction;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;

/**********************************************************************
 * &lt;p&gt;文件名：SomeBeanIntroductionInterceptor.java &lt;/p&gt;
 * &lt;p&gt;文件描述：${DESCRIPTION}(描述该文件做什么)
 * @project_name：spring
 * @author zengshunyao
 * @date 2019/2/21 19:51
 * @history
 * @department：政务事业部
 * Copyright ChengDu Funi Cloud Code Technology Development CO.,LTD 2014
 *                    All Rights Reserved.
 */
public class SomeBeanIntroductionInterceptor implements IOtherBean, IntroductionInterceptor {

    public void doOther() {
        System.out.println("doOther!");
    }

    public Object invoke(MethodInvocation invocation) throws Throwable {
        //判断调用的方法是否为指定类中的方法
        if (implementsInterface(invocation.getMethod().getDeclaringClass())) {
            return invocation.getMethod().invoke(this, invocation.getArguments());
        }
        return invocation.proceed();
    }

    /**
     * 判断clazz是否为给定接口IOtherBean的实现
     */
    @Override
    public boolean implementsInterface(Class clazz) {
        return clazz.isAssignableFrom(IOtherBean.class);
    }
}
